package com.hemi.rhet;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.hemi.rhet.html.TopicList;
import com.hemi.rhet.util.ApplicationContext;
import com.hemi.rhet.util.HttpUtil;
import com.hemi.rhet.util.OSInfoUtil;

public class ActivityMessage_TopicDetail extends Activity{
	private Handler mHandler = new Handler();   
	LinearLayout layout = null;
	WebView mWebView = null;
	String defaultUrl = null;
	String postId = null;
	String replyId=  null;
	private boolean isCreate =false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
    	OSInfoUtil.version4Adapter();
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.topic_detail);   
        mWebView = (WebView) findViewById(R.id.topic_detail_web_view); 
        WebSettings webSettings = mWebView.getSettings();   
        webSettings.setDefaultTextEncodingName("utf-8"); 
        webSettings.setSavePassword(false);   
        webSettings.setSaveFormData(false);   
        webSettings.setJavaScriptEnabled(true);   
        webSettings.setSupportZoom(false);   
        webSettings.setBuiltInZoomControls(false);
        Intent  i =  getIntent();
        replyId=  i.getExtras().getString("msgKeyValue");
        mWebView.setInitialScale(100);//初始显示比例100%
        mWebView.setMinimumHeight(100);
        mWebView.setMinimumWidth(100);
       String param =  i.getExtras().getString("NOTIFICATION_URI");
      try {
		HashMap paramMapd=   HttpUtil.buildJSON2Map(param);
		postId = (String) paramMapd.get("postId");
	} catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
        JavaScriptInterfaceBinder.INSTANCE.addCallCaremaAblity(this, mWebView, mHandler);
        JavaScriptInterfaceBinder.INSTANCE.addWebDataPostAblity(this, mWebView, mHandler);
        JavaScriptInterfaceBinder.INSTANCE.addWebDataUrlRedirectAblity(this, mWebView, mHandler);
        JavaScriptInterfaceBinder.INSTANCE.addWebImageSelectAblity(this, mWebView, mHandler);
    	mWebView.addJavascriptInterface(new JavaScriptInterface2HiddenProcessBar(), "hiddenProcessBar");   
    	isCreate = true;
        new ShowTopicList().execute();
        ImageView topic_send = (ImageView)findViewById(R.id.topic_send);
        topic_send.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent();
				i.putExtra("postId", postId);
				i.setClass(ActivityMessage_TopicDetail.this, ActivityTopicReply.class);
				startActivityForResult(i,11111);
			}
		});
        ImageView im = (ImageView)findViewById(R.id.topic_close);
		im.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	} 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(ActivityTopicReply.doReply==resultCode)
		{
			new ShowTopicList().execute();
		}
	}
	public void hiddenProcess(){
		if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
      		 mLoadingDialog.dismiss();
      		 mLoadingDialog = null;
      	 }
	}
	 class JavaScriptInterface2HiddenProcessBar {
	        public void clickOnAndroid() {   
	        	hiddenProcess();
	        }   
	    }
	private ProgressDialog mLoadingDialog;
private class ShowTopicList extends AsyncTask<Void, Void, Object> {
    @Override
    protected void onPreExecute() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.setMessage("加载中...");
        }else{
	       	 mLoadingDialog = new ProgressDialog(ActivityMessage_TopicDetail.this);
	       	 mLoadingDialog.setCancelable(false);
	       	 mLoadingDialog.setMessage("加载中...");
	         mLoadingDialog.show();
        }
    }
    @Override
    protected Object doInBackground(Void... params) {
 	   String urlStr = new StringBuffer(ApplicationContext.DOMAIN+"/post/repdetail_4json").toString();
 			try {
 				HashMap mapparams=  new HashMap();
 				mapparams.put("reply.id", replyId);
 				String  data = HttpUtil.postUrl(urlStr, mapparams);
 				String datahtml=TopicList.createNew().compile(ActivityMessage_TopicDetail.this,"post_detail.template", data);
 				return datahtml;
 			} catch (ClientProtocolException e) {
 				e.printStackTrace();
 				return e;
 			} catch (IOException e) {
 				e.printStackTrace();
 				return e;
 			}finally{
 			}
    }
    @Override
    protected void onPostExecute(Object result) { 
        if(result!=null &&	(result instanceof String))
        {
        	mWebView.loadDataWithBaseURL("file:///android_asset/.", result.toString(), "text/html", "utf-8", null);  
        }else if(result instanceof Exception){ 
      	  	Toast.makeText(ActivityMessage_TopicDetail.this, "数据加载失败！！",Toast.LENGTH_SHORT).show();            
      	  hiddenProcess();
		  } 
    }
}
protected void onResume() {
	super.onResume();
	if(isCreate)
	{
		isCreate=false;
	}else{
		// new ShowTopicList().execute();
	}
}
	
	
}
